﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base.Organization;
    using UFIDA.U9.Base.UOM;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.Complete.CompleteRpt;
    using UFIDA.U9.MO.Complete;
    using UFIDA.U9.MO.Enums;
    using UFIDA.U9.MO.MO;
    using UFIDA.U9.MO.MOWorkHourBE;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// MOWorkHourSaveSV partial 
	/// </summary>	
	public partial class MOWorkHourSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new MOWorkHourSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class MOWorkHourSaveSVImpementStrategy : BaseStrategy
	{
		public MOWorkHourSaveSVImpementStrategy() { }

		
		public override object Do(object obj)
		{						
			MOWorkHourSaveSV bpObj = (MOWorkHourSaveSV)obj;

			try
			{
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");

                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();
				
				foreach (var dto in bpObj.Data)
				{
					MOWorkHourBE item = null;
					string opath = "";
					if (dto.SrcDoc != null)
						//通过来源单据查找工时
						opath = $"CONVERT(varchar(100),BusinessDate, 23)='{dto.BusinessDate.Substring(0,10)}' and SrcDoc.SrcDocNo='{dto.SrcDoc.SrcDocNo}'";
					else if (dto.MO != null)
						//通过生产订单查找工时
						opath = $"CONVERT(varchar(100),BusinessDate, 23)='{dto.BusinessDate.Substring(0, 10)}' and MO.DocNo='{dto.MO.Code}'";

					item = MOWorkHourBE.Finder.Find(opath);
					if (item == null)
					{
						//新增逻辑
						item=MOWorkHourBase.CreateMOWorkHour(dto);
					}
					else
					{
						//修改逻辑
						item = MOWorkHourBase.UpdateMOWorkHour(item, dto);
					}

					retlist.Add(new CommonArchiveDataDTOData
					{
						ID = item.ID,
						Code = item.MO.DocNo
					}) ;
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

		
	}

	#endregion
	
	
}